/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.bi.web;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.mapper.JsonMapper;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import com.alibaba.fastjson.JSON;
import com.boxin.ims.modules.bi.entity.AliveMember;
import com.boxin.ims.modules.bi.service.AliveMemberService;
import com.boxin.ims.modules.wechat.entity.WechatMember;
import com.boxin.ims.modules.wechat.service.WechatMemberService;

/**
 * 每天活跃用户Controller
 * @author XiaoQian
 * @version 2014-06-19
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH+"/bi/aliveMember")
public class AliveMemberController extends BaseController {

	@Autowired
	private AliveMemberService aliveMemberService;
	
	@Autowired
	private WechatMemberService memberService;
	
	@ModelAttribute
	public AliveMember get(@RequestParam(required=false) Long id) {
		if (id != null){
			return aliveMemberService.get(id);
		}else{
			return new AliveMember();
		}
	}
	
	@RequestMapping(value = {"list", ""})
	public String list(AliveMember aliveMember, HttpServletRequest request, HttpServletResponse response, Model model) {
		User user = UserUtils.getUser();
		if (!user.isAdmin()){
			aliveMember.setUser(user);
		}
        Page<AliveMember> page = aliveMemberService.find(new Page<AliveMember>(request, response), aliveMember); 
        model.addAttribute("page", page);
		return "modules/bi/aliveMemberList";
	}

	@RequestMapping(value = "form")
	public String form(AliveMember aliveMember, Model model) {
	
		User user = UserUtils.getUser();
		if (aliveMember.getUser() == null || aliveMember.getUser().getId() <1){
			aliveMember.setUser(user);
		}
		
		model.addAttribute("aliveMember", aliveMember);
		return "modules/bi/aliveMemberForm";
	}

	@RequestMapping(value = "save")
	public String save(AliveMember aliveMember, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, aliveMember)){
			return form(aliveMember, model);
		}
		aliveMemberService.save(aliveMember);
		addMessage(redirectAttributes, "保存每天活跃用户'" + aliveMember.getName() + "'成功");
		return "redirect:"+Global.ADMIN_PATH+"/bi/aliveMember/?repage";
	}
	
	@RequestMapping(value = "delete")
	public String delete(Long id, RedirectAttributes redirectAttributes) {
		aliveMemberService.delete(id);
		addMessage(redirectAttributes, "删除每天活跃用户成功");
		return "redirect:"+Global.ADMIN_PATH+"/bi/aliveMember/?repage";
	}
	
	@ResponseBody
	@RequestMapping(value = "getMemberBy")
	public String getMemberBy(HttpServletRequest request,Long id, RedirectAttributes redirectAttributes) {
		String wechatId=request.getParameter("wechatId");
		User user = UserUtils.getUser();
		WechatMember member=  memberService.getByWechatMemberId(user, wechatId);
		
		Map<String, String> map =new HashMap<String, String>();
		map.put("name", member.getName());
		map.put("mobile", member.getMobile());
		
		return JSON.toJSONString(map);
	}

}
